package main

import (
	"database/sql"
	"fmt"
	"strconv"
)

type BidBlockChain struct{
	//区块链，存储区块顺序和结构
	blockList []bidBlock
	db *sql.DB
}

//赋值数据库指针
func NewBidBlockChain(db *sql.DB) *BidBlockChain {
	bc := new(BidBlockChain)
	bc.db = db
	return bc
}

//向区块链中添加区块
func (l *BidBlockChain)AddBlock(db *sql.DB, b *bidBlock){
	b.LinkBid(db)
	b.Seal()
	l.Validate(b)
	l.blockList = append(l.blockList, *b)
	a := InsertBid(l.db, strconv.FormatInt(b.EventID, 10),
		b.prevHash, b.hash, b.nodeMAC, b.nodeIP, b.nodePe)
	b.BlockID = int(a)
}

//验证区块链
func (l *BidBlockChain)Validate(b *bidBlock){
	for i,block := range l.blockList{
		block.Validate()
		err := recover() //获取异常
		if err != nil {
			fmt.Println("第" + strconv.Itoa(i) + "块区块内容被篡改")
		}
	}
}